﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Kaidou.Persister;
using Kaidou.Model;
using System.Collections.Generic;
using Kaidou.Util;
using Kaidou.Bll;

public partial class master_master_branch : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        LoginModel loginModel = (LoginModel)Session["loginModel"];

        if (loginModel == null)
        {
            Response.Redirect("../login.aspx");
        }
        else if (!loginModel.isOperator())
        {
            Response.Redirect("../menu.aspx");
        }

        if ("30".Equals(MstUnion.USER_CLSS_OPERATOR))
        {
            loginButton.Visible = true;
        }
        else
        {
            loginButton.Visible = false;
        }

        string area = Resources.Resource.area;
        string[] areaArray = area.Split(',');
        for (int i = 0; i < areaArray.Length; i++)
        {
            ListItem item = new ListItem();
            item.Value = (i + 1) + "";
            item.Text = areaArray[i];
            office_pref_code.Items.Insert(i + 1, item);
        }
    }

    protected void login_confirm(object sender, EventArgs e)
    {
        //モデルに値をセットする。
        mst_union_branch M = new mst_union_branch();
        mst_union_branchId MID = new mst_union_branchId();
        if (!ParameterCheckUtility.isNumber(branch_code.Text.Trim()))
        {
            MID.branchcode = -1;
        }
        else
        {
            MID.branchcode = branch_code.Text.Trim().Length == 0 ? (short)(-1) : short.Parse(branch_code.Text);
        }
        MID.unioncode = union_code.Text;
        M.Id = MID;
        M.officeaddress = office_address.Text;
        M.officecity = office_city.Text;
        M.officeemail = office_email.Text;
        M.officefax = office_fax.Text;
        M.officekana = office_kana.Text;
        M.officename = office_name.Text;
        M.officeprefcode = short.Parse(office_pref_code.Text);
        M.officetel = office_tel.Text;
        M.officezipno = office_zip_no1.Text + office_zip_no2.Text;
        M.officezipno1 = office_zip_no1.Text;
        M.officezipno2 = office_zip_no2.Text;
        M.password = StringUtil.getRandomPassword();
        M.regdate = System.DateTime.Now;
        M.tantokana = tanto_kana.Text;
        M.tantoname = tanto_name.Text;
        
        M.upddate = System.DateTime.Now;
        M.updunioncode = (string)Session["Login_Union_code"];
        M.updbranchcode = (short)Session["Login_Branch_code"];

        //本店の情報を取得
        MST_UnionPersister dao = new MST_UnionPersister();
        MstUnion parentUnion = dao.getDetail(M.Id.unioncode);
        //本店の区分を支店にコピーする
        M.userclss = parentUnion==null?null:parentUnion.UserClss;

        M.cancelrestflg = StringUtil.CANCEL_REST_FLG_NORMAL;
        M.unionsts = StringUtil.STATUS_NORMAL;
        M.updfuncid = "insertBranch()";

        //検証
        MST_Union_BranchManager validation = new MST_Union_BranchManager();
        List<string> errorCodeList = validation.checkInsertOrUpdateBranch(M, Resources.Resource.ResourceManager, true);
        msg.Text = ErrorCodeUtil.errorCodeListToHtml(errorCodeList);

        if (errorCodeList.Count == 0)
        {
            Session["mstunionbranch"] = M;
            Response.Redirect("master_branch_conf.aspx");
        }
    }
   
}
